package com.ruoyi.web.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.JoinWrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.web.dto.PileDto;
import com.ruoyi.web.entity.Station;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import com.ruoyi.web.entity.Pile;
import com.ruoyi.web.service.IPileService;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-01-26
 */
@RestController
@RequestMapping("/admin/pile")
public class PileController extends BaseController {

    private final IPileService pileService;
    public PileController(IPileService pileServiceImpl){
        this.pileService = pileServiceImpl;
    }
    /**
     * 查询电桩管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:pile:list')")
    @GetMapping("/list")
    public TableDataInfo list(Pile pile)
    {
        startPage();
        List<PileDto> list = pileService.selectPileList(pile);
        return getDataTable(list);
    }

    @GetMapping("/getDatas/{id}")
    public AjaxResult getDatas(@PathVariable("id") Long id) {
        List<Pile> list = pileService.list(Wrappers.lambdaQuery(Pile.class)
                .eq(Pile::getStationId,id));
        List<Map<String, Object>> datas = new ArrayList<>();
        for (Pile pile : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("pileId", pile.getId().toString());
            map.put("pileName", pile.getPileName());
            datas.add(map);
        }
        return success(datas);
    }

    /**
     * 导出电桩管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:pile:export')")
    @Log(title = "电桩管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Pile pile)
    {
        List<Pile> list = pileService.list();
        ExcelUtil<Pile> util = new ExcelUtil<Pile>(Pile.class);
        util.exportExcel(response, list, "电桩管理数据");
    }

    /**
     * 获取电桩管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:pile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pileService.selectPileById(id));
    }

    /**
     * 新增电桩管理
     */
    @PreAuthorize("@ss.hasPermi('admin:pile:add')")
    @Log(title = "电桩管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Pile pile) {
        pile.setId(IdUtil.getSnowflakeNextId());
        return toAjax(pileService.insertPile(pile));
    }

    /**
     * 修改电桩管理
     */
    @PreAuthorize("@ss.hasPermi('admin:pile:edit')")
    @Log(title = "电桩管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Pile pile) {
        return toAjax(pileService.updatePile(pile));
    }

    /**
     * 删除电桩管理
     */
    @PreAuthorize("@ss.hasPermi('admin:pile:remove')")
    @Log(title = "电桩管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pileService.deletePileByIds(ids));
    }
}
